import React from 'react';
import {Button, Radio, Menu, Dropdown, Table} from 'antd';
import './FilePage.css';
import { DownloadOutlined, ReadOutlined, SyncOutlined} from '@ant-design/icons';
import store from "../../store/store";
import {getFile, getModule} from "../../util/login";
import API  from '../../api/api'
import getFiles from "../../api/directories/getFiles";
var preIcon = <ReadOutlined/>;
var downLoadIcon = <DownloadOutlined/>;

let data = [];

const columns = [
    {
        title: '文件名称',
        dataIndex: 'name',
        key: 'name',
        render: (text,record) => <a onClick={ _=>{
            API.getFileContent(record.key);
        }
        }>
            {text}
        </a>
    },
    {
        title: '文件类型',
        dataIndex: 'type',
        key: 'type'
    },
    {
        title: '操作',
        dataIndex: 'Options',
        key: 'Options',
        render: (text,record) => (
            <div>
                {/* <SyncOutlined spin /><ReadOutlined /> */}
                <Button type="primary" shape="round" icon={preIcon}
                        style={{marginRight: "1rem", border: "0.1rem solid #ff7875"}} danger>
                    收藏
                </Button>
                <Button type="primary" shape="round" icon={downLoadIcon} size="middle">
                    下载
                </Button>
            </div>
        ),
    },
];

class FilePage extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data:[],
            uuid: this.uuid()
        }
    }

    uuid() {
        var s = [];
        var hexDigits = "0123456789abcdef";
        for (var i = 0; i < 36; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
        s[8] = s[13] = s[18] = s[23] = "-";
    
        var uuid = s.join("");
        return uuid;
    }

    storeFresh(){
        let tmpData = getFiles(store.getState().moduleId);
        this.pushFileData(tmpData)
        return <Table columns={columns} dataSource={data} size="middle"/>
    }

    pushFileData(tmpData){
        if(tmpData == null){
            return
        }
        let localData = [];
        for (let i = 0; i <tmpData.length ; i++) {
            let node = tmpData[i];
            let id = node.fileId;
            let name = node.fileName;
            let type;
            if(node.fileType == "file"){
                type = "文件"
            }
            else{
                type = "文章"
            }
            localData.push({
                key: id,
                name: name,
                type: type
            });
        }
        data = localData;
    }

    render(){
        return (
            <div>
                {this.storeFresh()}
            </div>
        )
    }
}


export default FilePage